//
//  UIImageGifView.m
//  云成绩
//
//  Created by apple on 16/7/19.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "UIImageGifView.h"


@implementation UIImageGifView

{
    
    UIImageView *imgV;

    UILabel *lableText;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
 */
- (void)imageGif:(int )type color:(UIColor *)color text:(NSString *)text frame:(CGRect)frame superView:(UIView *)view {
    
    [self imageGifEnd];
    self.frame = frame;
    CGFloat height = CGRectGetHeight(frame);

    self.backgroundColor = color;
    
    view = view ?: [UIApplication sharedApplication].windows.firstObject;
    
    [view addSubview:self];
    
    imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
    imgV.tag = 99008;
    imgV.frame = CGRectMake((KWidth -100 * sWidth) / 2, (height-100 * sWidth)/2, 100 * sWidth, 100 * sWidth);
    NSMutableArray *ary = [[NSMutableArray alloc] init];
    int i = 0;
    for (; i<9; i++) {
        UIImage *m = [UIImage imageNamed:[ NSString stringWithFormat:@"%d",i+1]];
        [ary addObject:m];
    }
    //获取图片视图
    //设置图片视图的动画图片数组
    imgV.animationImages = ary;
    //设置动画效果   让图片视图切换图片
    //完成一次动画需要的时间
    imgV.animationDuration = ary.count/9;
    //设置重复次数
    imgV.animationRepeatCount = 1000;
    [imgV startAnimating];
    //     progressHUD.customView =imgV;
    //     //    [progressHUD show:YES];
    //     [progressHUD showAnimated:YES];
    [self addSubview:imgV];
    
    lableText = [[UILabel alloc] init];
    lableText.frame = CGRectMake(0, (height - 100 * sWidth) / 2 + 100 * sWidth, KWidth, 40);
    if (text == nil || [text isEqualToString:@""]) {
        lableText.text = @"玩命加载中...";
    }else{
        lableText.text = text;
    }
    lableText.textColor = [UIColor colorWithHexString:@"#656D78"] ;
    lableText.font = [UIFont systemFontOfSize:13.0 * sWidth];
    lableText.textAlignment = 1;
    lableText.numberOfLines = 0;
    [self addSubview:lableText];
    
}

- (void)imageGif:(int )type color:(UIColor *)color text:(NSString *)text{
    CGRect frame;
    
    if (type == 123 ) {
        //下方有segmentbar，有tababar
        frame = CGRectMake(0, kNavibarH + 35, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - kNavibarH - 35 - kTabbarH);
    }else if (type == 12 ){
        //下方有segmentbar，无tababar
        frame = CGRectMake(0, kNavibarH + 35, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - kNavibarH - 35);
    }else if (type == 13 ){
        //导航栏以下，有tababar
        frame = CGRectMake(0, kNavibarH, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - kNavibarH - kTabbarH);
    }else if (type == 3 ){
        //无导航栏，有tababar
        frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - kTabbarH);
    }else if (type == 1 ){
        //导航栏以下，无tababar
        frame = CGRectMake(0, kNavibarH, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - kNavibarH);
    }else{
        //全屏展示
        frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    
    [self imageGif:type color:color text:text frame:frame superView:nil];
}

- (void)imageGifText:(NSString *)text{
    lableText.text = text;
}

- (void)dealloc {

    [imgV removeFromSuperview];
    imgV = nil;
    
    [self removeFromSuperview];
}


- (void)imageGifEnd {
    
    [imgV removeFromSuperview];
    imgV = nil;
    
    [self removeFromSuperview];
    
}

@end
